package com.caih.contacts.service.impl;



import com.caih.contacts.dao.DepartmentDao;
import com.caih.contacts.entity.Department;
import com.caih.contacts.entity.Result;
import com.caih.contacts.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class DepartmentServiceimpl implements DepartmentService {

    @Autowired
    private DepartmentDao dao;


    /**
     *
     * @return
     */
    @Override
    public Result<List<Department>>queryAll(){
        List<Department> list =dao.findAll();
        Result<List<Department>> result=new Result<>();

        if (list.size()>0){
            result.setCode(result.OK);
            result.setData(list);
            result.setMessage("查询成功");
        }
        else{
            result.setCode(result.ERROR);
            result.setMessage("查询无结果");
        }
        return result;
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public Result<Department>queryById(Integer id){
        Optional<Department> optionalDepartment =dao.findById(id);
        Result<Department> result = new Result<>();
        try {
            Department department =optionalDepartment.get();

            result.setCode(result.OK);
            result.setData(department);
            result.setMessage("查询成功");
        }catch (Exception e){
            result.setCode(result.ERROR);
            result.setMessage(e.toString());
        }
        return result;
    }

    /**
     * 方法带更新
     * @param dept
     * @return
     */
    @Override
    public Result<Department> update(Department dept) {
        Result<Department> result = new Result<>();
        Department department = dao.save(dept);
        result.setCode(Result.OK);
        result.setData(department);
        result.setMessage("更新");
        return result;
    }

    /**
     * 方法待更新
     * @param dept
     * @return
     */
    @Override
    public Result<Department> save(Department dept) {
        Result<Department> result = new Result<>();
        Department department = dao.save(dept);
        result.setCode(Result.OK);
        result.setData(department);
        result.setMessage("保存");
        return result;
    }

    /**
     * 删除通过id
     * @param id
     * @return
     */
    @Override
    public Result<String> deleteById(Integer id) {
        Result<String> result = new Result<>();
        result.setMessage("删除成功");
        result.setCode(Result.OK);
        dao.deleteById(id);
        return result;
    }

    /**
     *
     * @param parent_id
     * @return
     */
    @Override
    public Result<List<Department>>queryByParentid(Integer parent_id){
        Result<List<Department>> result = new Result<>();
      List<Department> department =  dao.queryByParentid(parent_id);
        if(department!=null){
            result.setMessage("查询成功");
            result.setCode(Result.OK);
            result.setData(department);
        }else {
            result.setCode(Result.ERROR);
            result.setMessage("查询无结果");
        }
        return result;
    }

}
